package com.ruoyi.combat.service.userlocation.impl;

import java.util.*;

import com.ruoyi.combat.domain.userlocation.CombatRelation;
import com.ruoyi.combat.mapper.userlocation.CombatRelationMapper;
import com.ruoyi.combat.mapper.userlocation.CombatUserMapper;
import com.ruoyi.combat.service.userlocation.ICombatRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户社交关系
 * Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-30
 */
@Service
public class CombatRelationServiceImpl implements ICombatRelationService {
    @Autowired
    private CombatRelationMapper combatRelationMapper;

    @Autowired
    private CombatUserMapper combatUserMapper;

    /**
     * 查询用户社交关系
     *
     * @param id 用户社交关系
     *           主键
     * @return 用户社交关系
     */
    @Override
    public CombatRelation selectCombatRelationById(Long id) {
        return combatRelationMapper.selectCombatRelationById(id);
    }

    /**
     * 查询用户社交关系
     * 列表
     *
     * @param combatRelation 用户社交关系
     * @return 用户社交关系
     */
    @Override
    public List<CombatRelation> selectCombatRelationList(CombatRelation combatRelation) {
        return combatRelationMapper.selectCombatRelationList(combatRelation);
    }

    /**
     * 新增用户社交关系
     *
     * @param combatRelation 用户社交关系
     * @return 结果
     */
    @Override
    public int insertCombatRelation(CombatRelation combatRelation) {
        return combatRelationMapper.insertCombatRelation(combatRelation);
    }

    /**
     * 修改用户社交关系
     *
     * @param combatRelation 用户社交关系
     * @return 结果
     */
    @Override
    public int updateCombatRelation(CombatRelation combatRelation) {
        return combatRelationMapper.updateCombatRelation(combatRelation);
    }

    /**
     * 批量删除用户社交关系
     *
     * @param ids 需要删除的用户社交关系
     *            主键
     * @return 结果
     */
    @Override
    public int deleteCombatRelationByIds(Long[] ids) {
        return combatRelationMapper.deleteCombatRelationByIds(ids);
    }

    /**
     * 删除用户社交关系
     * 信息
     *
     * @param id 用户社交关系
     *           主键
     * @return 结果
     */
    @Override
    public int deleteCombatRelationById(Long id) {
        return combatRelationMapper.deleteCombatRelationById(id);
    }

    @Override
    public List<CombatRelation> selectRelationByUserId(CombatRelation relation) {
        return combatRelationMapper.selectRelationByUserId(relation);
    }

    @Override
    public List<Map> getGraphRelation(String userid) {
        List<Map> result = new ArrayList<>();
        CombatRelation combatRelation = new CombatRelation();
        combatRelation.setUserid(userid);
        List<CombatRelation> relations = combatRelationMapper.selectRelationByUserId(combatRelation);
        for (CombatRelation relation : relations) {
            result.add(getRelationMap(userid, relation));
            String targetUserId = relation.getTargetid();
            CombatRelation targetRelation = new CombatRelation();
            targetRelation.setUserid(targetUserId);
            List<CombatRelation> targetRelations = combatRelationMapper.selectRelationByUserId(targetRelation);
            for (CombatRelation targetRel : targetRelations) {
                result.add(getRelationMap(targetUserId, targetRel));
            }
        }
        return result;
    }

    public Map getRelationMap(String userid, CombatRelation relation) {
        Map<String, String> relationMap = new HashMap<>();
        relationMap.put("fromId", relation.getUserid());
        relationMap.put("fromName", relation.getUsername());
        relationMap.put("fromCity", relation.getUserCity());
        relationMap.put("relation", relation.getRelation());
        relationMap.put("toId", relation.getTargetid());
        relationMap.put("toName", relation.getTargetName());
        relationMap.put("toCity", relation.getTargetCity());
        return relationMap;
    }
}
